using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using CE.Global;
using CE.Provider;
using CE.User;

public partial class Admin_provider_AssignProviderType : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session[CE.Global.General.UserId] != null && Session[CE.Global.General.UserTypeId] != null)
        {
            if (Convert.ToInt32(Session[CE.Global.General.UserTypeId]) !=
                Convert.ToInt32(CE.Global.CEUserType.SiteAdmin))
            {
                Response.Redirect("AdminSecureLogin.aspx");
            }
        }
        else
        {
            Response.Redirect("AdminSecureLogin.aspx");
        }

        if (!IsPostBack)
        {
            DataTable _dtProviderType = General.GetComboItem("TH.ProviderType", false);
            cblProviderType.DataSource = _dtProviderType;
            cblProviderType.DataTextField = _dtProviderType.Columns[0].ColumnName.ToString();
            cblProviderType.DataValueField = _dtProviderType.Columns[1].ColumnName.ToString();
            cblProviderType.DataBind();

            DataTable _dtCompany = General.GetComboItem("TH.ProviderCompany", false);
            ddlProviderCompanyName.DataSource = _dtCompany;
            ddlProviderCompanyName.DataTextField = _dtCompany.Columns[0].ColumnName.ToString();
            ddlProviderCompanyName.DataValueField = _dtCompany.Columns[1].ColumnName.ToString();
            ddlProviderCompanyName.DataBind();

            ddlProviderCompanyName.Items.Insert(0, new ListItem("Select", "-1"));
        }
    }
    protected void btnAddProviderType_Click(object sender, EventArgs e)
    {
        lblCheckList.Text = "";
        ProviderType _protype = new ProviderType();
        ProviderTypeDB _protypedb = new ProviderTypeDB();
        _protypedb.DeleteAllAssignment(Convert.ToInt64(ddlProviderCompanyName.SelectedValue), Convert.ToInt64(ddlProviderId.SelectedValue));
        foreach (ListItem lstItem in cblProviderType.Items)
        {
            if (lstItem.Selected)
            {
                lblCheckList.Text += lstItem.Text + " >> " + lstItem.Value + " ";
                _protype.CEID = Convert.ToInt64(ddlProviderId.SelectedValue);
                _protype.ProviderCompanyId = Convert.ToInt64(ddlProviderCompanyName.SelectedValue);
                _protype.ProviderTypeId = Convert.ToInt64(lstItem.Value);
                _protype.IsAllow = true;
                _protype.RefCEUserID = 0;
                _protype.Status = "NEW";
                lblCheckList.Text += _protypedb.AssignProviderType(_protype).ToString() + " <br />";
            }
        }
    }
    protected void ddlProviderCompanyName_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (ddlProviderCompanyName.SelectedValue != "-1")
        {
            ProviderCompanyLinkDB _procomdb = new ProviderCompanyLinkDB();
            DataTable _dtProvider = new DataTable("ProviderList");
            DataTable _dtList = new DataTable();
            _dtList.Columns.Add("Name");
            _dtList.Columns.Add("Email");

            DataTable _dtIDList = new DataTable();
            _dtIDList.Columns.Add("Name");
            _dtIDList.Columns.Add("CEID");

            _dtProvider = _procomdb.GetProviderList(Convert.ToInt64(ddlProviderCompanyName.SelectedValue));
            for (int i = 0; i < _dtProvider.Rows.Count; i++)
            {
                ProfileDB _profiledb = new ProfileDB();
                Profile _profile = new Profile();
                _profile = _profiledb.GetProfile(Convert.ToInt64(_dtProvider.Rows[i]["CEID"].ToString()));

                Contact _contact = new Contact();
                ContactDB _contactdb = new ContactDB();
                _contact = _contactdb.GetContact(Convert.ToInt64(_dtProvider.Rows[i]["CEID"].ToString()));

                if (_profile != null && _contact != null)
                {
                    DataRow _dtRow = _dtList.NewRow();
                    _dtRow["Name"] = _profile.Salutation + " " + _profile.SurName + " " + _profile.FirstName;
                    _dtRow["Email"] = _contact.Email;
                    _dtList.Rows.Add(_dtRow);

                    DataRow _dtIDRow = _dtIDList.NewRow();
                    _dtIDRow["Name"] = _profile.Salutation + " " + _profile.SurName + " " + _profile.FirstName;
                    _dtIDRow["CEID"] = _dtProvider.Rows[i]["CEID"].ToString();
                    _dtIDList.Rows.Add(_dtIDRow);
                }

                _profiledb = null;
                _contact = null;
            }
            ddlProviderName.DataSource = null;
            ddlProviderName.DataSource = _dtList;
            ddlProviderName.DataTextField = "Name";
            ddlProviderName.DataValueField = "Email";
            ddlProviderName.DataBind();

            ddlProviderId.DataSource = null;
            ddlProviderId.DataSource = _dtIDList;
            ddlProviderId.DataTextField = "Name";
            ddlProviderId.DataValueField = "CEID";
            ddlProviderId.DataBind();

            ddlProviderName.Items.Insert(0, new ListItem(" ", "-1"));
            ddlProviderId.Items.Insert(0, new ListItem(" ", "-1"));
            lblProviderEmailAddress.Text = "";
        }
        else
        {
            ddlProviderName.DataSource = null;
            lblProviderEmailAddress.Text = "";
            
        }
    }
    protected void ddlProviderName_SelectedIndexChanged(object sender, EventArgs e)
    {
        lblProviderEmailAddress.Text = ddlProviderName.SelectedValue;
        ddlProviderId.SelectedIndex = ddlProviderName.SelectedIndex;

        //Clear CheckBox list check item
        foreach (ListItem lstItem in cblProviderType.Items)
        {
                lstItem.Selected = false;
        }

        //Check Item
        if (ddlProviderId.SelectedValue != "-1")
        {
            DataTable _dtList = new DataTable("ProviderTypeList");
            ProviderTypeDB _protypedb = new ProviderTypeDB();
            _dtList = _protypedb.GetAssignmentList(Convert.ToInt64(ddlProviderCompanyName.SelectedValue), Convert.ToInt64(ddlProviderId.SelectedValue));
            if (_dtList != null)
            {
                if (_dtList.Rows.Count > 0)
                {
                    foreach (DataRow rValue in _dtList.Rows)
                    {
                        foreach (ListItem lstItem in cblProviderType.Items)
                        {
                            if (rValue[0].ToString() == lstItem.Value)
                            {
                                lstItem.Selected = true;
                                break;
                            }
                        }
                    }
                }
            }
        }
    }
    protected void btnHome_Click(object sender, EventArgs e)
    {
        Response.Redirect("../AdminHome.aspx");
    }
    protected void btnNext_Click(object sender, EventArgs e)
    {
        Response.Redirect("SendProviderConfirmation.aspx");
    }
}
